﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace VH.SharePoint.Api.WLW
{
    //Ref: http://msdn.microsoft.com/en-us/library/bb463260.aspx
    public class Supports
    {
        #region API Capabilities
        [XmlElement("clientType", IsNullable=false)]
        public ClientType ClientType { get; set; }

        [XmlElement("supportsPostAsDraft")]
        public SupportsFeature PostAsDraft { get; set; }

        [XmlElement("supportsFileUpload")]
        public SupportsFeature FileUpload { get; set; }

        [XmlElement("supportsExtendedEntries")]
        public SupportsFeature ExtendedEntries { get; set; }

        [XmlElement("supportsCustomDate")]
        public SupportsFeature CustomDate { get; set; }

        [XmlElement("supportsCustomDateUpdate")]
        public SupportsFeature CustomDateUpdate { get; set; }

        [XmlElement("supportsCategories")]
        public SupportsFeature Categories { get; set; }

        [XmlElement("supportsCategoriesInline")]
        public SupportsFeature CategoriesInline { get; set; }

        [XmlElement("supportsMultipleCategories")]
        public SupportsFeature MultipleCategories { get; set; }

        [XmlElement("supportsHierarchicalCategories")]
        public SupportsFeature HierarchicalCategories { get; set; }

        [XmlElement("supportsNewCategories")]
        public SupportsFeature NewCategories { get; set; }

        [XmlElement("supportsNewCategoriesInline")]
        public SupportsFeature NewCategoriesInline { get; set; }

        [XmlElement("maxCategoryNameLength")]
        public Int32 CategoryNameLength { get; set; }

        [XmlElement("supportsKeywords")]
        public SupportsFeature Keywords { get; set; }

        [XmlElement("supportsCommentPolicy")]
        public SupportsFeature CommentPolicy { get; set; }

        [XmlElement("supportsPingPolicy")]
        public SupportsFeature PingPolicy { get; set; }

        [XmlElement("supportsAuthor")]
        public SupportsFeature Author { get; set; }

        [XmlElement("supportsSlug")]
        public SupportsFeature Slug { get; set; }

        [XmlElement("supportsPassword")]
        public SupportsFeature Password { get; set; }

        [XmlElement("supportsExcerpt")]
        public SupportsFeature Excerpt { get; set; }

        [XmlElement("supportsTrackbacks")]
        public SupportsFeature Trackbacks { get; set; }

        [XmlElement("supportsPages")]
        public SupportsFeature Pages { get; set; }

        [XmlElement("supportsPageParent")]
        public SupportsFeature PageParent { get; set; }

        [XmlElement("supportsPageOrder")]
        public SupportsFeature PageOrder { get; set; }
        #endregion

        [XmlElement("supportsEmptyTitles")]
        public SupportsFeature EmptyTitles { get; set; }

        [XmlElement("requiresHtmlTitles")]
        public SupportsFeature HtmlTitle { get; set; }

        [XmlElement("maxPostTitleLength")]
        public Int32 TitleLength { get; set; }

        [XmlElement("requiresXHTML")]
        public SupportsFeature XHTML { get; set; }

        [XmlElement("supportsScripts")]
        public SupportsFeature Scripts { get; set; }

        [XmlElement("supportsEmbeds")]
        public SupportsFeature Embeds { get; set; }

        [XmlElement("characterSet")]
        public String CharSet { get; set; }

        [XmlElement("supportsAutoUpdate")]
        public SupportsFeature AutoUpdate { get; set; }

        public Supports()
        {
            this.EmptyTitles = SupportsFeature.No;
            this.HtmlTitle = SupportsFeature.No;
            this.XHTML = SupportsFeature.No;
            this.Scripts = SupportsFeature.Yes;
            this.Embeds = SupportsFeature.Yes;
            this.CharSet = "utf-8";
            this.AutoUpdate = SupportsFeature.Yes;
        }

        public static Supports Default(ClientType clientType)
        {
            switch (clientType)
            {
                case ClientType.Metaweblog:
                    return DefaultMetaweblog();
                case ClientType.MovableType:
                    return DefaultMovableType();
                case ClientType.WordPress:
                    return DefaultWordpress();
                case ClientType.SharePoint:
                    return DefaultSharePoint();
            }
            throw new NotSupportedException("This client type is not supported");
        }

        private static Supports DefaultSharePoint()
        {
            var opts = DefaultMetaweblog();
            opts.ClientType = ClientType.SharePoint;
            opts.EmptyTitles = SupportsFeature.No;
            opts.XHTML = SupportsFeature.Yes;
            opts.CustomDate = SupportsFeature.No;
            opts.CustomDate = SupportsFeature.No;
            opts.TitleLength = 100;
            opts.CategoryNameLength = 255;
            return opts;
        }

        private static Supports DefaultWordpress()
        {
            return new Supports()
                {
                    ClientType = ClientType.WordPress,
                    PostAsDraft = SupportsFeature.Yes,
                    FileUpload = SupportsFeature.Yes,
                    ExtendedEntries = SupportsFeature.Yes,
                    CustomDate = SupportsFeature.Yes,
                    Categories = SupportsFeature.Yes,
                    CategoriesInline = SupportsFeature.Yes,
                    MultipleCategories = SupportsFeature.Yes,
                    HierarchicalCategories = SupportsFeature.Yes,
                    NewCategories = SupportsFeature.Yes,
                    NewCategoriesInline = SupportsFeature.Yes,
                    CommentPolicy = SupportsFeature.Yes,
                    PingPolicy = SupportsFeature.Yes,
                    Author = SupportsFeature.Yes,
                    Slug = SupportsFeature.Yes,
                    Password = SupportsFeature.Yes,
                    Excerpt = SupportsFeature.Yes,
                    Trackbacks = SupportsFeature.Yes,
                    PageOrder = SupportsFeature.Yes,
                    PageParent = SupportsFeature.Yes,
                    Pages = SupportsFeature.Yes
                };
        }

        private static Supports DefaultMovableType()
        {
            return new Supports()
                {
                    ClientType = ClientType.MovableType,
                    PostAsDraft = SupportsFeature.Yes,
                    FileUpload = SupportsFeature.Yes,
                    ExtendedEntries = SupportsFeature.Yes,
                    CustomDate = SupportsFeature.Yes,
                    Categories = SupportsFeature.Yes,
                    MultipleCategories = SupportsFeature.Yes,
                    Keywords = SupportsFeature.Yes,
                    CommentPolicy = SupportsFeature.Yes,
                    PingPolicy = SupportsFeature.Yes,
                    Excerpt = SupportsFeature.Yes,
                    Trackbacks = SupportsFeature.Yes
                };
        }

        private static Supports DefaultMetaweblog()
        {
            return new Supports()
                {
                    ClientType = ClientType.Metaweblog,
                    PostAsDraft = SupportsFeature.Yes,
                    FileUpload = SupportsFeature.Yes,
                    CustomDate = SupportsFeature.Yes,
                    Categories = SupportsFeature.Yes,
                    CategoriesInline = SupportsFeature.Yes,
                    MultipleCategories = SupportsFeature.Yes
                };
        }

    }

    public enum SupportsFeature
    {
        No,
        Yes,
        Unknown
    }
}
